﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_detailPro_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
       // this.btnCancel.Attributes.Add("onclick", "return ClearAll()");
        if (!this.IsPostBack) {
            LoadCombobox();
            InitItem();
        }
    }
   
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
    }

    private void AddNew()
    {
        String strMess = "";
        int id = 0;
        int ID = -1;
        try
        {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
            WebData db = new WebData();
            DataLibrary.tbl_DetailProduct item = new DataLibrary.tbl_DetailProduct();
            item.Description = txtDescription.Text;
            item.Id = ID;
            item.IdProduct = Convert.ToInt32(cbbidProduct.SelectedValue);
            item.Description = txtDescription.Text;
            item.OrderId = Convert.ToInt32(txtorderId.Text);
            item.Name = txtname.Text;
            item.IsActive = 1;
            id = db.tbl_DetailProducts.UpdateOnSubmit(item);

        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
        if (id > 0)
            Response.Redirect(ViewState["URLReferrer"].ToString());
      
    }

    private void LoadCombobox(){

        WebData db = new WebData();
        this.cbbidProduct.DataSource = db.tbl_Products.GetAll();
        this.cbbidProduct.DataBind();
    }

    private void InitItem()
    {
        int ID=-1;
        try {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        WebData db=new WebData();
        DataLibrary.tbl_DetailProduct item = db.tbl_DetailProducts.GetItemById(ID);
        if (item != null) {
            txtDescription.Text = item.Description;
            txtname.Text = item.Name;
            txtorderId.Text = item.OrderId.ToString();
            cbbidProduct.SelectedValue = item.IdProduct.ToString();
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
}